#!/usr/bin/python3

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib, GdkPixbuf

import subprocess
from subprocess import Popen, PIPE

import os
import sys

SELFDIR=os.path.dirname(os.path.realpath(__file__))+"/"

def cmdexec(cmd1="",cmd2=""):
  global win
  cmd=cmd1
  if cmd2!="": cmd=cmd2
  cmd=cmd.strip()
  if cmd.endswith('&'):
    print(cmd)
    cmd=cmd[:-1]
    print(cmd)
    subprocess.Popen(cmd,shell=True)
    return
  else:
    a=subprocess.getoutput(cmd)
    return a

class MyWindow(Gtk.Window):    
  def __init__(self):
    global SELFDIR,ONLINE
    super().__init__(title="XLinux Hilfecenter")
    if os.path.isfile(SELFDIR+"help.png"):
      self.set_icon_from_file(SELFDIR+"help.png")
    self.set_position(Gtk.WindowPosition.CENTER)
    grid = Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    symbolsize=60

    if not ONLINE:
      label=Gtk.Label(halign=3,justify=2);
      label.set_markup("<b>Keine Verbindung zum Internet!</b>\nDaher sind einige Funktionen nicht verfügbar.")
      grid.attach(label,0,0,2,1)

    
    # Kurzeinweisung
    btn0 = Gtk.Button(xalign=0)
    btn0.connect("clicked", self.wincmd, "/opt/XLinux/Hilfe/Einweisung/bildergalerie.py &")
    btn0_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn0_label=Gtk.Label(label="Kurz-Einführung",xalign=0.5);
    btn0_img=Gtk.Image()
    btn0_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"kurzeinweisung.svg",width=symbolsize, height=symbolsize, preserve_aspect_ratio=True))
    btn0_grid.attach(btn0_img, 0, 0, 1, 1)
    btn0_grid.attach(btn0_label, 0, 1, 1, 1)
    btn0.add(btn0_grid)
    grid.attach(btn0, 0, 1, 1, 1)
    if not os.path.isfile("/opt/XLinux/Hilfe/Einweisung/bildergalerie.py"):
      btn0.set_sensitive(False)

    btn1 = Gtk.Button(xalign=0)
    btn1.connect("clicked", self.wincmd, "exo-open '/opt/XLinux/Hilfe/WLAN verbinden.pdf' &")
    btn1_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn1_label=Gtk.Label(label="WLAN verbinden",xalign=0.5);
    btn1_img=Gtk.Image()
    btn1_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"wifi.svg",width=symbolsize, height=symbolsize, preserve_aspect_ratio=True))
    btn1_grid.attach(btn1_img, 0, 0, 1, 1)
    btn1_grid.attach(btn1_label, 0, 1, 1, 1)
    btn1.add(btn1_grid)
    grid.attach(btn1, 1, 1, 1, 1)

    btn2 = Gtk.Button(xalign=0)
    btn2.connect("clicked", self.wincmd, "exo-open https://support.mastro-it.de/service?uuid="+cmdexec("xlinux-systemid")+" &")
    btn2_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn2_label=Gtk.Label(label="Hilfe & Anleitungen",xalign=0.5);
    btn2_img=Gtk.Image()
    btn2_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"onlinehilfe.svg",width=symbolsize, height=symbolsize, preserve_aspect_ratio=True))
    btn2_grid.attach(btn2_img, 0, 0, 1, 1)
    btn2_grid.attach(btn2_label, 0, 1, 1, 1)
    btn2.add(btn2_grid)
    grid.attach(btn2, 0, 2, 1, 1)
    if not ONLINE:
      btn2.set_sensitive(False)

    btn3 = Gtk.Button(xalign=0)
    btn3.connect("clicked", self.wincmd, "/opt/XLinux-Fernwartung/2022_fernwartung.sh &")
    btn3_grid=Gtk.Grid(margin=10,column_spacing=10,row_spacing=10,hexpand=True,column_homogeneous=True,row_homogeneous=False)
    btn3_label=Gtk.Label(label="kostenpflichtige Fernwartung",xalign=0.5);
    btn3_img=Gtk.Image()
    btn3_img.set_from_pixbuf(GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=SELFDIR+"fernwartung.svg",width=symbolsize, height=symbolsize, preserve_aspect_ratio=True))
    btn3_grid.attach(btn3_img, 0, 0, 1, 1)
    btn3_grid.attach(btn3_label, 0, 1, 1, 1)
    btn3.add(btn3_grid)
    grid.attach(btn3, 1, 2, 1, 1)
    if not ONLINE:
      btn3.set_sensitive(False)

    self.add(grid);

  def wincmd(self,button,befehl):
    cmdexec(befehl)
    Gtk.main_quit()

ONLINE=False
if cmdexec("xlinux-checkonline").strip()=="ja":
  ONLINE=True

win=MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
